/**
	This file is part of Fighting Field.

	Fighting Field is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Fighting Field is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Fighting Field.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ParamsPanel.h"

#include "OgreTextAreaOverlayElement.h"

namespace FF
{
	ParamsPanel::ParamsPanel(const Ogre::String& name, const Style& style, Ogre::Real width, unsigned int lines)
		: mStyle(style)
	{
		mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
			(mStyle.templateName, "BorderPanel", name);
		Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
		mNamesArea = (Ogre::TextAreaOverlayElement*)c->getChild(GetName() + "/ParamsPanelNames");
		mValuesArea = (Ogre::TextAreaOverlayElement*)c->getChild(GetName() + "/ParamsPanelValues");
		mNamesArea->setCharHeight(mNamesArea->getCharHeight() - 3);
		mValuesArea->setCharHeight(mValuesArea->getCharHeight() - 3);
		mElement->setWidth(width);
		mElement->setHeight(mNamesArea->getTop() * 2 + lines * mNamesArea->getCharHeight());
	}

	void ParamsPanel::SetAllParamNames(const Ogre::StringVector& paramNames)
	{
		mNames = paramNames;
		mValues.clear();
		mValues.resize(mNames.size(), "");
		mElement->setHeight(mNamesArea->getTop() * 2 + mNames.size() * mNamesArea->getCharHeight());
		UpdateText();
	}

	const Ogre::StringVector& ParamsPanel::GetAllParamNames()
	{
		return mNames;
	}

	void ParamsPanel::SetAllParamValues(const Ogre::StringVector& paramValues)
	{
		mValues = paramValues;
		mValues.resize(mNames.size(), "");
		UpdateText();
	}

	void ParamsPanel::SetParamValue(const Ogre::DisplayString& paramName, const Ogre::DisplayString& paramValue)
	{
		for (unsigned int i = 0; i < mNames.size(); i++)
		{
			if (mNames[i] == paramName.asUTF8())
			{
				mValues[i] = paramValue.asUTF8();
				UpdateText();
				return;
			}
		}

		Ogre::String desc = "ParamsPanel \"" + GetName() + "\" has no parameter \"" + paramName.asUTF8() + "\".";
		OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::setParamValue");
	}

	void ParamsPanel::SetParamValue(unsigned int index, const Ogre::DisplayString& paramValue)
	{
		if (index >= mNames.size())
		{
			Ogre::String desc = "ParamsPanel \"" + GetName() + "\" has no parameter at position " +
				Ogre::StringConverter::toString(index) + ".";
			OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::setParamValue");
		}

		mValues[index] = paramValue.asUTF8();
		UpdateText();
	}

	Ogre::DisplayString ParamsPanel::GetParamValue(const Ogre::DisplayString& paramName)
	{
		for (unsigned int i = 0; i < mNames.size(); i++)
		{
			if (mNames[i] == paramName.asUTF8()) return mValues[i];
		}

		Ogre::String desc = "ParamsPanel \"" + GetName() + "\" has no parameter \"" + paramName.asUTF8() + "\".";
		OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::getParamValue");
		return "";
	}

	Ogre::DisplayString ParamsPanel::GetParamValue(unsigned int index)
	{
		if (index >= mNames.size())
		{
			Ogre::String desc = "ParamsPanel \"" + GetName() + "\" has no parameter at position " +
				Ogre::StringConverter::toString(index) + ".";
			OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::getParamValue");
		}

		return mValues[index];
	}

	const Ogre::StringVector& ParamsPanel::GetAllParamValues()
	{
		return mValues;
	}

	void ParamsPanel::UpdateText()
	{
		Ogre::DisplayString namesDS;
		Ogre::DisplayString valuesDS;

		for (unsigned int i = 0; i < mNames.size(); i++)
		{
			namesDS.append(mNames[i] + ":\n");
			valuesDS.append(mValues[i] + "\n");
		}

		mNamesArea->setCaption(namesDS);
		mValuesArea->setCaption(valuesDS);
	}
}
